package com.example.testnetwork;
import android.annotation.SuppressLint;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import com.example.testnetwork.receiver.AlarmCheckNetworkReceiver;

/**
 * 定时任务管理，主要创建AlarmManager，间隔HEART_INTERVAL分钟执行一次任务：“发送广播到AlarmCheckNetworkReceiver”
 */
public class AlarmManager {

    //private  long HEART_INTERVAL = 1 * 20 * 1000;//闹钟执行任务的时间间隔
    private  long HEART_INTERVAL = 4 * 60 * 1000;//闹钟执行任务的时间间隔
    private Context mContext;
    public static android.app.AlarmManager mAlarmManager;
    public static PendingIntent pendingIntent;
    private static AlarmManager instance = null;
    String TAG = "AlarmManager";

    private AlarmManager(Context aContext) {
        this.mContext = aContext;
    }

    /**
     * 单例获取AlarmManager对象
     * @param aContext
     * @return
     */
    public static AlarmManager getInstance(Context aContext) {
        if (instance == null) {
            synchronized (AlarmManager.class) {
                if (instance == null) {
                    instance = new AlarmManager(aContext);
                }
            }
        }
        return instance;
    }

    /**
     * 创建闹钟定时器的pendingIntent:闹钟定时器 到达时 发送广播 MyConstants.ALARM_CHECK_NET 到AlarmCheckNetworkReceiver.class
     * @return
     */
    public PendingIntent createBroadcastPendingIntent()
    {
        Intent intent = new Intent(MyConstants.ALARM_CHECK_NET);
        intent.setClass(mContext, AlarmCheckNetworkReceiver.class);
        pendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
        return pendingIntent;
    }

    /**
     *  创建mAlarmManager，并为它附加一个pendingIntent：定时发广播
     */
    public void createAlarmManager() {
       if(mAlarmManager != null && pendingIntent != null)
        {
            mAlarmManager.cancel(pendingIntent);
        }
        mAlarmManager = (android.app.AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
        createBroadcastPendingIntent();
    }

    /**
     * 第一次 启动 闹钟定时器，延迟1秒后启动
     */
    @SuppressLint("NewApi")
    public void startAlarmManager() {
        long alarmTime =  System.currentTimeMillis() + 1000;
        AlarmCheckNetworkReceiver.lastSendTime = alarmTime;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            android.app.AlarmManager.AlarmClockInfo alarmClockInfo = new android.app.AlarmManager.AlarmClockInfo(alarmTime,pendingIntent );
            mAlarmManager.setAlarmClock(alarmClockInfo,pendingIntent);
        }
        else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            mAlarmManager.setExact(android.app.AlarmManager.RTC_WAKEUP, alarmTime, pendingIntent);
        } else {
            mAlarmManager.setRepeating(android.app.AlarmManager.RTC_WAKEUP, alarmTime, HEART_INTERVAL, pendingIntent);
        }
    }

    /**
     * 这个函数在AlarmCheckNetworkReceiver广播里调用，实现循环定时，即每隔HEART_INTERVAL分钟执行一次定时任务：“发广播”，然后在广播里再次调用该函数，依次循环。
     */
    @SuppressLint("NewApi")
    public void startAlarmManagerWorkOnReceiver() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
           android.app.AlarmManager.AlarmClockInfo alarmClockInfo = new android.app.AlarmManager.AlarmClockInfo(System.currentTimeMillis() + HEART_INTERVAL,null );
           if(mAlarmManager == null) {
               mAlarmManager = (android.app.AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
           }
           if(pendingIntent == null) {
               pendingIntent = createBroadcastPendingIntent();
           }
            mAlarmManager.setAlarmClock(alarmClockInfo,pendingIntent);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            mAlarmManager.setExact(android.app.AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + HEART_INTERVAL, pendingIntent);
        }
    }
}